package com.example.agent.controller;

import com.example.agent.entity.ChatInfo;
import com.example.agent.entity.MessageVO;
import com.example.agent.repository.ChatHistoryRepository;
import com.google.j2objc.annotations.Property;
import jakarta.annotation.Resource;
import lombok.extern.slf4j.Slf4j;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.ai.chat.client.advisor.AbstractChatMemoryAdvisor;
import org.springframework.ai.chat.memory.ChatMemory;
import org.springframework.ai.chat.messages.Message;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;
import java.util.stream.Collectors;

@Slf4j
@RestController
@RequestMapping("/chat")
public class ChatController {
    @Autowired
    private ChatClient chatClient;

//    @Autowired
//    @Qualifier("dpChatClient")
//    private ChatClient chatClient;

    @Autowired
    private ChatHistoryRepository inMemoryRepository;

    @Autowired
     private ChatMemory chatMemory;

//    @RequestMapping(value = "/stream",produces = "text/html;charset=utf-8")
//    public Flux<String> stream(@RequestParam("prompt") String prompt) {
//        return chatClient.prompt()
//                .user(prompt)
//                .stream()
//                .content();
//    }


    @RequestMapping(value = "/stream",produces = "text/html;charset=utf-8")
    public Flux<String> stream(@RequestParam("prompt") String prompt, @RequestParam("chatId") String chatId) {
        log.info("[stream] chatId:{}",chatId);
        inMemoryRepository.save(chatId,prompt);
        return chatClient.prompt()
                .user(prompt)
                .advisors(spec->spec.param(AbstractChatMemoryAdvisor.CHAT_MEMORY_CONVERSATION_ID_KEY,chatId))
                .stream()
                .content();
    }

    /*
    会话列表
     */
    @RequestMapping("/getChatIds")
    public List<ChatInfo> getChatIds() {
        return inMemoryRepository.getChats();
    }

    /*
    获取会话历史
     */
    @RequestMapping("/getChatHistory")
    public List<MessageVO> getChatHistory(@RequestParam("chatId") String chatId) {
        log.info("[getChatHistory] chatId:{}",chatId);
        List<Message> messages = chatMemory.get(chatId, 20);
//        messages.stream().map(e->new MessageVO(e.getRole().name(),e.getContent()));
        log.info("[getChatHistory] history:{}",messages.stream().map(MessageVO::new).collect(Collectors.toList()));
        return messages.stream().map(MessageVO::new).collect(Collectors.toList());
    }

    /*
    删除会话
     */
    @RequestMapping("/deleteChat")
    public Boolean deleteChat(@RequestParam("chatId") String chatId) {
        log.info("[deleteChat] chatId:{}",chatId);
        try {
            inMemoryRepository.cleanByChatId(chatId);
            chatMemory.clear(chatId);
        } catch (Exception e) {
            return false;
        }
        return true;
    }
}
